package com.jf.blog.controller;

import com.jf.blog.bean.Category;
import com.jf.blog.form.category.CategoryForm;
import com.jf.blog.form.category.CategorySearchForm;
import com.jf.blog.service.CategoryService;
import com.jf.blog.util.ResultJson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 博客分类表 前端控制器
 * </p>
 *
 * @author JF
 * @since 2025-06-20
 */
@RestController
@RequestMapping("/category")
@Api(tags = "分类模块")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping("/update")
    @ApiOperation("添加或修改")
    public ResultJson update(@RequestBody CategoryForm categoryForm) {
        Category category = new Category();
        /*转换form为实体类*/
        BeanUtils.copyProperties(categoryForm, category);
        categoryService.saveOrUpdate(category);
        return ResultJson.success();
    }

    @GetMapping("/info/{categoryId}")
    @ApiOperation("查看详情")
    public ResultJson info(@PathVariable int categoryId) {
        return ResultJson.success(categoryService.getById(categoryId));
    }

    @DeleteMapping("/delete")
    @ApiOperation("批量删除")
    public ResultJson delete(@RequestBody List<Integer> categoryIds) {
        categoryService.delete(categoryIds);
        return ResultJson.success();
    }

    @GetMapping("/page")
    @ApiOperation("分页筛选")
    public ResultJson page(@RequestParam(defaultValue = "1") @ApiParam(required = true) int page, @RequestParam(defaultValue = "5") @ApiParam(required = true) int limit, CategorySearchForm searchForm) {
        return categoryService.getPage(page, limit, searchForm);
    }

    @GetMapping("/select")
    @ApiOperation("下拉框数据")
    public ResultJson select() {
        return categoryService.select();
    }
}
